package com.highspeed.bdk.gateway.socket.analyze.message.gui.base;

import com.highspeed.bdk.gateway.Util;
import com.highspeed.bdk.gateway.socket.analyze.base.AbstractResolver;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

public abstract class MessageResolver extends AbstractResolver {

    @Override
    protected Map doResolveHeader(byte[] response) {
        //解析前12个字节数为头部
        short startTag = Util.getShort(response, getOffset());
        offset2();
        logger.info("开始标志............." + startTag);

        short reqpacketId = Util.getShort(response, getOffset());
        offset2();
        logger.info("请求序号............." + reqpacketId);

        short fbpacketId = Util.getShort(response, getOffset());
        offset2();
        logger.info("反馈包的序列号............." + fbpacketId);

        short fbOperType = Util.getShort(response, getOffset());
        offset2();
        logger.info("反馈包的操作号............." + fbOperType);

        int dataLen = Util.getInt(response, getOffset());
        offset4();
        logger.info("数据长度............." + dataLen);

        return getContainer();
    }

    @Override
    protected Map doResolveTail(byte[] response) {
        return getContainer();
    }

    /**
     * 解析存储文件部分
     *
     * @param response
     * @param fileName
     * @return
     */
    protected int resolveFileBody(byte[] response, String fileName, int offset) {
        {
            String classpath = this.getClass().getResource("/").getPath().replaceFirst("/", "");
            String docRoot = classpath.replaceAll("target/classes/", "fbdata");
            //在项目路径下创建
            StringBuffer filePath = new StringBuffer(docRoot);
            filePath.append("/");
            //
//        filePath.append(luckHeader.getFbOperType());
//        filePath.append("/");
            //请求包序列号
            short reqpacketId = Util.getShort(response, 2);
            //只拼接请求包序列号
            filePath.append(reqpacketId);
            Util.createFile(filePath.toString(), fileName.toString());
            filePath.append(fileName);
            FileOutputStream fileOutputStream = null;
            try {
                //获取内容长度
                int length = Util.getInt(response, 8);
                offset += length;
                fileOutputStream = new FileOutputStream(new File(filePath.toString()));
                //写入内容
                fileOutputStream.write(response, 12, length);
                fileOutputStream.flush();
            } catch (FileNotFoundException e) {
                logger.error("未找到文件出错" + filePath + "//" + fileName);
            } catch (IOException e) {
                logger.error("文件异常" + fileName);

            } finally {
                try {
                    if (fileOutputStream != null) {
                        fileOutputStream.close();
                    }
                } catch (IOException e) {
                    logger.error("保存文件出错" + fileName);
                }
            }
        }
        return offset;
    }
}
